/**
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-11-24 22:18:21
 * Copyright (c) 2015- joysuch-版权所有
 */

package com.joysuch.wwyt.risk.repository;

import com.joysuch.wwyt.risk.entity.RiskEvaluationAlarmMesage;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;

import java.util.Date;
import java.util.List;

/**
 * RiskEvaluationAlarmMesageDao
 */
public interface RiskEvaluationAlarmMesageDao
        extends JpaRepository<RiskEvaluationAlarmMesage, Long>, QuerydslPredicateExecutor<RiskEvaluationAlarmMesage> {
    public Page<RiskEvaluationAlarmMesage> findAll(Specification<RiskEvaluationAlarmMesage> spec, Pageable pageable);

    public RiskEvaluationAlarmMesage save(RiskEvaluationAlarmMesage bean);

    public void delete(RiskEvaluationAlarmMesage bean);

    public List<RiskEvaluationAlarmMesage> findByCreateTimeAfter(Date time);

    int countByCreateTimeAfter(Date time);

    @Query(value = "SELECT COUNT(1) FROM wwyt_risk_evaluation_alarm_mesage WHERE RISK_DISTRICT_ID=?1 AND state =?2 AND DELETE_FLAG='0'", nativeQuery = true)
    int countDistrictNumber(Long id, int i);

    @Query(value = "SELECT DISTINCT RISK_DISTRICT_ID FROM wwyt_risk_evaluation_alarm_mesage WHERE STATE!=?1 and DELETE_FLAG = '0'", nativeQuery = true)
    List<Long> findDistrictIdsByState(int state);

    @Query(value = "select * from wwyt_risk_evaluation_alarm_mesage where risk_district_id =?1 and delete_flag = '0' and state!=1 order by id desc limit 1", nativeQuery = true)
    RiskEvaluationAlarmMesage getLastAlarmMeaasgeByDistrictId(Long districtId);

    @Modifying
    @Query(value = "UPDATE wwyt_risk_evaluation_alarm_mesage SET STATE = 1 ,HANDLE_REMARK = ?1 ,ALARM_REASON =?2 ,HANDLE_BY =?3,UPDATE_TIME =?4 WHERE DELETE_FLAG = '0' AND  STATE !=1", nativeQuery = true)
    void setAllMeaageHandled(String msg, String alarmReason, Long handleBy, Date updateTime);

    @Query(value = "select count(*) from wwyt_risk_evaluation_alarm_mesage where delete_flag = '0' and state=0 and tenent_id =?1 and org_code =?2", nativeQuery = true)
    int countByState(Long tenentId, String orgCode);

    @Query(value = "SELECT count(1) FROM wwyt_risk_evaluation_alarm_mesage WHERE   state =0 AND DELETE_FLAG='0' limit 1", nativeQuery = true)
    int checkCount(Long tenentId, String orgCode);
}
